package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.entity.DepartmentInfo;
import com.example.prison.service.DepartmentInfoService;
import com.example.prison.mapper.DepartmentInfoMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author jiang
* @description 针对表【department_info】的数据库操作Service实现
* @createDate 2023-05-26 14:36:03
*/
@Service
public class DepartmentInfoServiceImpl extends ServiceImpl<DepartmentInfoMapper, DepartmentInfo>
    implements DepartmentInfoService{

    @Autowired
    private DepartmentInfoMapper departmentInfoMapper;

    @Override
    public PageInfo<DepartmentInfo> get(int pageNumber, int pageSize, String departmentName ) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(departmentInfoMapper.get(departmentName));
    }

    @Override
    public int add(DepartmentInfo department) {
        String name = department.getDepartmentName();
        if (ObjectUtils.isNotEmpty(departmentInfoMapper.getOneByName(name))) {
            return -1;
        } else {
            departmentInfoMapper.add(department);
            return 1;
        }
    }

    @Override
    public void delete(Integer[] ids) {
        departmentInfoMapper.deleteDepartment(ids);
    }

    @Override
    public int update(DepartmentInfo department, Integer id) {
        DepartmentInfo prison = departmentInfoMapper.getById(1); //监狱
        DepartmentInfo police = departmentInfoMapper.getById(2); //武警

        int otherId;
        if(id == 1) {
            otherId = 2;
        } else {
            otherId = 1;
        }
        DepartmentInfo otherDepartment = departmentInfoMapper.getById(otherId);
        if (department.getRemove() == 1) {
            otherDepartment.setRemove(0);
        }
        if (department.getProcess() == 1) {
            otherDepartment.setProcess(0);
        }

        departmentInfoMapper.updateDepartment(department, id);
        List<DepartmentInfo> DepartmentInfoList = departmentInfoMapper.getByName(department.getDepartmentName());
        if(DepartmentInfoList.size() > 1) {
            departmentInfoMapper.updateDepartmentFail(prison, 1);
            departmentInfoMapper.updateDepartmentFail(police, 2);
            return -1;
        } else {
            departmentInfoMapper.updateDepartment(department,id);
            departmentInfoMapper.updateDepartment(otherDepartment,otherId);
            return 1;
        }
    }

    @Override
    public DepartmentInfo getById(Integer id) {
        return departmentInfoMapper.getById(id);
    }


}




